/**
 * Created by wzl on 2i17/4/16.
 */
/**
 * [ 'landuse',
 'water',
 'barrier_line',
 'building',
 'road',
 'rail_station_label',
 'poi_label',
 'road_label',
 'housenum_label',
 'landcover',
 'hillshade',
 'contour' ]
 */

let VectorTile = require('./src/thirdparty/vector-tile-js').VectorTile;
// let VectorTile = require('vector-tile').VectorTile;
let Protobuf = require('pbf');
let fs = require('fs');

let data=fs.readFileSync('24640.vector.pbf');
let pbf = new Protobuf(data);
let tile = new VectorTile(pbf);

// Contains a map of all layers
console.log(Object.keys(tile.layers));

let building = tile.layers.water;

// Amount of features in this layer
console.log(building.name);
console.log(building.version);
console.log(building.extent);
console.log(building.length);

let arr=[];
for(let i=0;i<building.length;i++){
    console.log('-----------------');
    // console.log(building.feature(i));
    // console.log(building.feature(i).id);
    console.log(building.feature(i).type);//['Unknown', 'Point', 'LineString', 'Polygon']
    // console.log(building.feature(i).extent);
    console.log(building.feature(i).properties);
    // console.log(building.feature(i).loadGeometry());
    // arr.push(building.feature(i).toGeoJSON(54885,26776,16));
    // console.log(building.feature(i).bbox());
    // let geom = building.feature(i).loadGeometry();
    // let geojson =building.feature(i).toGeoJSON(54885,26776,16);
    // geojson.geometry.coordinates.forEach((shape)=>{
    //     console.log(shape);
    //     console.log(isClockWiseGeoJSON(shape)?'ClockWise':'Counter-ClockWise');
    // })
    // geom.forEach((shape)=>{
    //     console.log(shape);
    //     console.log(isClockWise(shape)?'ClockWise':'Counter-ClockWise');
    // })
}

// fs.writeFileSync('geometry.geojson',JSON.stringify(arr));
// console.log(building.feature(i).toGeoJSON());

function isClockWiseGeoJSON(coords){
    let sum=0;
    for (let i=0;i<coords.length-1;i++){
        sum+=(coords[i+1][0] - coords[i][0]) * (coords[i+1][1] + coords[i][1]);
    }
    // console.log('sum: ',sum);
    return sum>0;
}
function isClockWise(coords){
    let sum=0;
    for (let i=0;i<coords.length-1;i++){
        sum+=(coords[i+1].x - coords[i].x) * (coords[i+1].y + coords[i].y);
    }
    console.log(sum);
    return sum>0;
}